package cn.com.nes.site.controller.crm.sale;

import cn.com.nes.common.web.CommonResult;
import cn.com.nes.mybatis.agent.crm.entity.Opportunity;
import cn.com.nes.site.controller.crm.opportunity.vo.ExportTemplateExcelOfEndCustomerVO;
import cn.com.nes.site.controller.crm.opportunity.vo.ImportExcelOfEndCustomerVO;
import cn.com.nes.site.controller.crm.sale.vo.GovernmentSaleActivityVO;
import cn.com.nes.site.controller.crm.sale.vo.GovernmentSaleDetailVO;
import cn.com.nes.site.controller.crm.sale.vo.GovernmentSaleVO;
import cn.com.nes.site.controller.crm.sale.vo.RetailSaleVO;
import cn.com.nes.site.modules.excel.EndCustomerSyncReadListener;
import cn.com.nes.site.modules.excel.dto.EndCustomerDTO;
import cn.com.nes.site.modules.sale.SaleService;
import cn.com.nes.site.modules.sale.bo.SaleDetailBO;
import cn.com.nes.site.service.crm.opportunity.CrmOpportunityService;
import com.alibaba.excel.EasyExcel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

@Slf4j
@Api(tags = {"销售管理-销售"})
@RestController
@RequestMapping("/v2/crm/sale")
public class SaleController {

    @Resource
    SaleService saleService;

    @Resource
    private CrmOpportunityService crmOpportunityService;

    @ApiOperation(value = "政府公共商务项目-终端客户销售详情", notes = "查询销售数据详情")
    @RequestMapping(value = "/getGovernmentSaleDetail", method = RequestMethod.GET)
    public CommonResult<?> getGovernmentSaleDetail(GovernmentSaleDetailVO vo) {
        return CommonResult.data(saleService.getSaleDetail(vo.convertToBo(SaleDetailBO.class)));
    }

    @ApiOperation(value = "零售销售", notes = "商务项目-零售销售信息保存")
    @RequestMapping(value = "/retailSale", method = RequestMethod.POST)
    public CommonResult<?> retailSale(
            @RequestBody @Validated RetailSaleVO retailSaleVO) throws Exception {
        saleService.saleSaveHandle(retailSaleVO.buildSaleBO());
        return CommonResult.ok();
    }

    @ApiOperation(value = "政府公共商务项目-终端客户销售", notes = "政府公共商务项目-终端客户tab，销售信息保存")
    @RequestMapping(value = "/governmentSaleForOpportunity", method = RequestMethod.POST)
    public CommonResult<?> governmentSaleForOpportunity(
            @RequestBody @Validated GovernmentSaleVO governmentSaleVO) throws Exception {
        saleService.saleSaveHandle(governmentSaleVO.buildSaleBO());
        return CommonResult.ok();
    }

    @ApiOperation(value = "政府活动-政府终端客户销售", notes = "政府申报活动页，销售信息保存")
    @RequestMapping(value = "/governmentSaleForActivity", method = RequestMethod.POST)
    public CommonResult<?> governmentSaleForActivity(
            @RequestBody @Validated GovernmentSaleActivityVO governmentSaleActivityVO) throws Exception {
        saleService.saleSaveHandle(governmentSaleActivityVO.buildSaleBO());
        return CommonResult.ok();
    }

    @ApiOperation(value = "政府公共商务项目终端客户销售excel导入")
    @RequestMapping(value = "/governmentEndCustomerSaleForExcelImport", method = RequestMethod.POST)
    public CommonResult<?> governmentEndCustomerSaleForExcelImport(
            @RequestParam(value = "file", required = false) MultipartFile file,
            @Validated ImportExcelOfEndCustomerVO vo) throws Exception {
        try {
            Opportunity opportunity = crmOpportunityService.getOpportunity(vo.getMainOpportunityUuid());
            vo.setOpportunityName(opportunity.getOpportunityName());

            Thread thread = new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        EasyExcel.read(file.getInputStream(), EndCustomerDTO.class, new EndCustomerSyncReadListener(vo, saleService))
                                .sheet(0)
                                .doRead();
                    } catch (Exception e) {
                        log.error("", e);
                        throw new RuntimeException(e);
                    }
                }
            });
            thread.start();

        } catch (Exception e) {
            return CommonResult.error("导入失败!" + e.getMessage());
        }
        return CommonResult.ok("导入完成");
    }

    @ApiOperation("导出终端客户销售Excel模版")
    @RequestMapping(value = "/exportEndCustomerSaleTemplateExcel", method = RequestMethod.POST)
    public CommonResult<?> exportEndCustomerSaleTemplateExcel(
            @RequestBody @Validated ExportTemplateExcelOfEndCustomerVO vo,
            HttpServletResponse response) throws Exception {
        try {
            saleService.exportEndCustomerSaleTemplateExcel(vo, response);
        } catch (Exception e) {
            log.error("下载导入模板失败,失败原因:{}", e.getMessage());
            return CommonResult.error("下载导入模板失败：" + e.getMessage());
        }
        return CommonResult.ok("模版导出成功");
    }

}
